﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using Com.Lancetinc.Evs.Resources;
using Com.Lancetinc.Evs.Services;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 报告 截图预览
    /// </summary>
    public partial class ReportImagePreviewUserControl
    {
        public ExperInfo ExperInfo { get; private set; }

        public event EventHandler<string> ImageSelected;

        public ReportImagePreviewUserControl()
        {
            InitializeComponent();
        }

        private int _currentImageIndex = -1;
        private string _currentImagePath = null;

        public ObservableCollection<string> Images { get; private set; }


        public void Setup(ExperInfo experInfo)
        {
            this.ExperInfo = experInfo;
            var images = experInfo.Images;
            Images = images;
            images.CollectionChanged += Images_CollectionChanged;

            // 加载时预览第一张图片
            BtnPreview_OnClick(null, null);
        }

        private void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (_currentImagePath == null)
                return;

            var idxBk = _currentImageIndex;
            _currentImageIndex = Images.IndexOf(_currentImagePath);
            if (idxBk != _currentImageIndex)
            {
                RefreshImage();
            }
        }

        private void RefreshImage()
        {
            if (_currentImageIndex < 0 || _currentImageIndex > Images.Count - 1)
            {
                _currentImagePath = null;
                LblCurrentImage.Text = null;
                ImgSc.Source = null;
                return;
            }

            var imgName = Images[_currentImageIndex];
            _currentImagePath = ExperInfo.GetImageFullPath(imgName);
            LblCurrentImage.Text = imgName;
            ImgSc.Source = ResourceHelper.GetImageFromFile(_currentImagePath);
        }

        private void BtnInsert_OnClick(object sender, RoutedEventArgs e)
        {
            if (_currentImagePath == null)
                return;

            ImageSelected?.Invoke(this, _currentImagePath);
        }

        private void BtnPreview_OnClick(object sender, RoutedEventArgs e)
        {
            if (_currentImagePath != null)
                return;

            if (!HasImage()) return;

            _currentImageIndex = 0;
            RefreshImage();
        }

        private void BtnPrevious_OnClick(object sender, RoutedEventArgs e)
        {
            if (!HasImage()) return;

            if (_currentImageIndex == 0)
                return;

            if (_currentImageIndex < 0)
                _currentImageIndex = 0;
            else
                _currentImageIndex--;

            RefreshImage();
        }

        private void BtnNext_OnClick(object sender, RoutedEventArgs e)
        {
            if (!HasImage()) return;

            if (_currentImageIndex >= Images.Count - 1)
                return;

            _currentImageIndex++;

            RefreshImage();
        }

        private bool HasImage()
        {
            return Images.Count > 0;
        }
    }
}